package com.wy.tingshu.view_object;

import com.wy.tingshu.data_object.AlbumDO;
import com.wy.tingshu.data_object.TrackDO;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author 美女
 * @date 2022/07/20 09:01
 **/

/**
 * 专辑+专辑相关的音频列表
 * 需要取：专辑的基本信息；已绑定音频列表
 * {
 *    "currentUser":...
 *    "aid":...
 *    "title":...
 *    "trackList":[{"tid":...,"title":...},...]
 *     //trackList:已绑定的音频列表
 * }
 */

@Data
public class BindView {
    public static class TrackView{
        public Integer tid;
        public String title;

        public TrackView(Integer tid, String title) {
            this.tid=tid;
            this.title=title;
        }
    }
    public UserVO currentUser;
    public Integer aid;
    public String title;
    public List<TrackView> trackList;

    public BindView(UserVO currentUser, AlbumDO albumDO, List<TrackDO> trackDOList) {
        this.currentUser=currentUser;
        this.aid=albumDO.aid;
        this.title=albumDO.title;
        //trackDOList写法1
//        this.trackList=new ArrayList<>();
//        for (TrackDO trackDO :trackDOList) {
//            TrackView view=new TrackView(trackDO.tid,trackDO.title);
//            trackList.add(view);
//        }
        //trackDOList写法2-匿名内部类
//        Stream<TrackDO> stream=trackDOList.stream();//调用stream方法得到stream流
//        Stream<TrackView> stream2=(Stream<TrackView>) stream.map(new Function<TrackDO, TrackView>() {
//            @Override
//            public TrackView apply(TrackDO trackDO) {
//                return new TrackView(trackDO.tid,trackDO.title);
//            }
//        });//调用流下的map方法,传入匿名函数
//        //上面的意思是会把流（由列表转过来的，所以也是把列表）中的所有元素
//        // 调用new Function<TrackDO, TrackView>这个接口，构造成匿名对象
//        List<TrackDO> collect=stream.collect(Collectors.toList());

        //trackDOList写法3-链式写法
        List<TrackView> collect = trackDOList.stream().map(d -> new TrackView(d.tid, d.title)).collect(Collectors.toList());
        this.trackList=collect;

    }
}
